<?php
/**
 * Created by PhpStorm.
 * User: liuxiu
 * Date: 2022/7/15
 * Time: 09:59
 */

namespace search\controllers\bigdata;


use business\elasticsearch\services\BigDataDeveloperService;

class DeveloperController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/developer/get-list 01、大数据开发商搜索
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataDeveloper
     *
     * @apiDescription 大数据开发商搜索
     *
     * @apiParam {String} keyword                           搜索词
     * @apiParam {String} province                          省份
     * @apiParam {String} city                              城市
     * @apiParam {String} area                              省份城市的混合查询，or条件，即：a省或b市
     * @apiParam {String} company_type                      企业类型
     * @apiParam {String} project_type                      项目类型
     * @apiParam {String} land_type                         土地类型（如果是多个，使用英文逗号拼接  eg: A,B）
     * @apiParam {String} developer_acticity_label          活跃采购商信息
     * @apiParam {String} developer_top_100_label           地产百强信息
     * @apiParam {String} is_new_decorate_expand_label      新精装客群信息
     * @apiParam {String} is_new_land_usage_expand_label    新住宅客群信息
     * @apiParam {String} is_new_area_expand_label          新区域拓展信息
     * @apiParam {String} high_project_customers_label      高端项目客群信息
     * @apiParam {String} industry_category                 行业分类
     * @apiParam {String} page                              页码
     * @apiParam {String} pageSize                          分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.organizing_code      社会统一信用码
     * @apiSuccess {String}  data.list.developer_name       采购商公司名
     * @apiSuccess {String}  data.list.short_name           简称
     * @apiSuccess {String}  data.list.developer_id         平台开发商id
     * @apiSuccess {String}  data.list.developer_logo       平台开发商logo
     * @apiSuccess {String}  data.list.legal_name           法人
     * @apiSuccess {String}  data.list.regist_capi          注册资金(单位万元)
     * @apiSuccess {String}  data.list.rec_capi             实缴资金(单位万元)
     * @apiSuccess {String}  data.list.establish_date       成立时间
     * @apiSuccess {String}  data.list.term_start           营业开始时间
     * @apiSuccess {String}  data.list.term_end             营业结束时间
     * @apiSuccess {String}  data.list.status               经营状态
     * @apiSuccess {String}  data.list.enterprise_nature    企业类型
     * @apiSuccess {String}  data.list.scope                经营范围
     * @apiSuccess {String}  data.list.province             所在省份
     * @apiSuccess {String}  data.list.city                 所在城市
     * @apiSuccess {String}  data.list.address              企业地址
     * @apiSuccess {Number}  data.list.number_of_employees  公司规模
     * @apiSuccess {Number}  data.list.taken_land_area      拿地指标数据
     * @apiSuccess {Number}  data.list.open_project_num     开盘指标数据
     * @apiSuccess {Number}  data.list.purchase_demand_num  需求指标数据
     * @apiSuccess {Number}  data.list.cooperation_enterprise_num  合作单位指标数据
     * @apiSuccess {String}  data.list.developer_nameHL     高亮公司名
     *
     * @apiSuccessExample {json} 成功响应：
     * {"success":true,"message":null,"errorCode":null,"data":{"resultNum":2,"resultSet":[{"organizing_code":"91440300MA5FELW74N",
     * "developer_name":"华夏幸福（深圳）运营管理有限公司","short_name":"华夏幸福运营管理","developer_id":"K008653","developer_logo":"","legal_name":"俞建","regist_capi":"100000万人民币","rec_capi":"100000万人民币","establish_date":"2018-12-20","term_start":"2018-12-20 00:00:00","term_end":null,
     * "status":"存续","enterprise_nature":"有限责任公司（法人独资）",
     * "scope":"一般经营项目是：企业管理咨询、商业运营管理咨询、体育信息咨询、投资咨询、商务信息咨询、酒店管理咨询（均 不含限制项目）；投资兴办实业（具体项目另行申报）；供应链管理服务；国内贸易（不含专营、专卖、专控商品）；经营进出口业务（法律、行政法规、国务院决定禁止的项目除外，限制的项目须取得许可后方可经营）。，许可经营项目是：建筑工程的设计、安装、施工及相关的工程信息咨询；物业管理。","province":"gd","city":"深圳","address":"深圳市前海深港合作区前湾一路1号A栋201室（入驻深圳市前海商务秘书有限公司）","number_of_employees":0,"nameSuggest":"华夏幸福（深圳）运营管理有限公司","developer_nameHL":"<em class=\"_high\">华夏</em><em class=\"_high\">幸福</em>（深圳）运营管理有限公司"},{"organizing_code":"91440101340072705Q","developer_name":"幸福时代生态城镇开发有限公司","short_name":"幸福时代生态城镇开发","developer_id":"K004494","developer_logo":"/company/K004494.200x100.jpg?v=55cae9abec7e5ebaad5270b7","legal_name":"严兵","regist_capi":"10000万人民币","rec_capi":"420万人民币","establish_date":"2015-05-29","term_start":"2015-05-29 00:00:00","term_end":null,"status":"存续","enterprise_nature":"有限责任公司（自然人投资或控股的法人独资）","scope":"房地产开发经营；旅游景区规划设计、开发、管理；自有房地产经营活动；教育咨询；商务咨询（不含投资咨询）。（依法须经批准的项目，经相关部门批准后方可开展经营活动）。","province":"sc","city":"223","address":"中国（四川）自由贸易试验区成都高新区吉庆三路333号1栋3单元25楼2505","number_of_employees":0,"nameSuggest":"幸福时代生态城镇开发有限公司","developer_nameHL":"<em class=\"_high\">幸福</em>时代生态城镇开发有限公司"}]}}
     *
     */
    public function actionGetList()
    {
        try {
            $keyword = $this->getParam('keyword');
            $province = $this->getParam('province');
            $city = $this->getParam('city');
            $area = trim($this->getParam('area')) ? explode(',', trim($this->getParam('area'))) : [];
            $companyType = $this->getParam('company_type');
            $projectType = $this->getParam('project_type');
            $landType = trim($this->getParam('land_type')) ? explode(',', trim($this->getParam('land_type'))) : [];
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            // 企业标签
            $tags = [];
            $tags['developer_top_100_label'] = $this->getParam('developer_top_100_label');
            $tags['is_new_decorate_expand_label'] = $this->getParam('is_new_decorate_expand_label');
            $tags['is_new_land_usage_expand_label'] = $this->getParam('is_new_land_usage_expand_label');
            $tags['is_new_area_expand_label'] = $this->getParam('is_new_area_expand_label');
            $tags['high_project_customers_label'] = $this->getParam('high_project_customers_label');

            // 企业等级
            $companyLevel = [];
            $companyLevel['developer_activity_label'] = $this->getParam('developer_activity_label');
            $companyLevel['is_ycg_certification'] = $this->getParam('is_ycg_certification');
            $companyLevel['is_ycg_offline_business_connect'] = $this->getParam('is_ycg_offline_business_connect');

            // 用户订阅信息
            $subscription = [];
            $subscription['land_usage_list'] = trim($this->getParam('subscription_land_usage')) ? explode(',', trim($this->getParam('subscription_land_usage'))) : [];
            $subscription['project_type_list'] = trim($this->getParam('subscription_project_type')) ? explode(',', trim($this->getParam('subscription_project_type'))) : [];

            //行业分类
            $industryCategoryMiddle = array_filter(explode(",", $this->getParam('industry_category_middle')));
            $companyTypeArr = !empty($companyType) ? explode(",", $companyType) : [];
            $province = !empty($province) ? explode(",", $province) : [];
            $city = !empty($city) ? explode(",", $city) : [];

            $bigDataDeveloperService = new BigDataDeveloperService();
            $rs = $bigDataDeveloperService->getDataList($keyword, $area, $province, $city, $companyTypeArr,
                $projectType, $landType, $tags, $companyLevel, $industryCategoryMiddle, $subscription, $page, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/developer/guess-names 02、大数据开发商名联想
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataDeveloper
     *
     * @apiDescription 大数据开发商名联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.developer_nameHL     搜索词高亮公司名
     * @apiSuccess {String}  data.list.developer        采购商公司名
     *
     *
     */
    public function actionGuessNames()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $bigDataDeveloperService = new BigDataDeveloperService();
            $rs = $bigDataDeveloperService->guessNames($keyword, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/developer/recommended-developer-list 03、省市雷达推荐客户列表接口
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataDeveloper
     *
     * @apiDescription 省市雷达推荐客户列表接口
     *
     * @apiParam {String} keyword               搜索词
     * @apiParam {String} province_code         省份code，多选，使用英文逗号拼接
     * @apiParam {String} city_code             城市code，多选，使用英文逗号拼接
     * @apiParam {String} area_code             省份、城市code，多选，使用英文逗号拼接,查询时是或者的关系 A省或B市
     * @apiParam {String} project_label         在建类型，多选，使用英文逗号拼接
     * @apiParam {String} land_usage            土地类型，多选，使用英文逗号拼接
     * @apiParam {String} start_time            时间范围查询的起始时间
     * @apiParam {String} end_time              时间范围查询的结束时间
     * @apiParam {String} page                  页码
     * @apiParam {String} page_size             分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     *
     *
     */
    public function actionRecommendedDeveloperList()
    {
        try {
            $keywords = $this->getParam('keyword') ? explode(',', trim($this->getParam('keyword'))) : [];
            $provinceCodes = $this->getParam('province_code') ? explode(',', trim($this->getParam('province_code'))) : [];
            $cityCodes = $this->getParam('city_code') ? explode(',', trim($this->getParam('city_code'))) : [];
            $areaCodes = $this->getParam('area_code') ? explode(',', trim($this->getParam('area_code'))) : [];
            $projectLabels = $this->getParam('project_label') ? explode(',', trim($this->getParam('project_label'))) : [];
            $landUsageLabels = trim($this->getParam('land_usage')) ? explode(',', trim($this->getParam('land_usage'))) : [];
            $fitmentFeeLabels = trim($this->getParam('fitment_fee_label')) ? explode(',', trim($this->getParam('fitment_fee_label'))) : [];
            $isPriceGtAvg = trim($this->getParam('is_price_gt_avg')) ? explode(',', trim($this->getParam('is_price_gt_avg'))) : [];
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $startTime = $this->getParam('start_time');
            $endTime = $this->getParam('end_time');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $areaCodes = array_filter(array_unique(array_merge($areaCodes, $provinceCodes, $cityCodes)));
            $bigDataDeveloperService = new BigDataDeveloperService();
            $rs = $bigDataDeveloperService->getRecommendedDeveloperList($keywords, $areaCodes,
                $projectLabels, $landUsageLabels, $fitmentFeeLabels, $isPriceGtAvg, $startTime, $endTime,
                $page, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
//            var_dump($ex->getMessage());
            \Yii::error("actionRecommendedDeveloperList exception message:" . $ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/developer/get-developer-score 04、获取采购商的好客户得分
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataDeveloper
     *
     * @apiDescription 获取采购商的好客户得分
     *
     * @apiParam {String} organizing_code               社会信用代码，多选，使用英文逗号拼接
     * @apiParam {String} subscription_land_usage       用户订阅的土地用途，多个用英文逗号拼接
     * @apiParam {String} subscription_project_type     用户订阅设置的项目在建类型，多个用英文逗号拼接
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     *
     *
     */
    public function actionGetDeveloperScore()
    {
        try {
            $organizingCodes = $this->getParam('organizing_code') ? explode(',', trim($this->getParam('organizing_code'))) : [];
            $subscription = [];
            $subscription['land_usage_list'] = trim($this->getParam('subscription_land_usage')) ? explode(',', trim($this->getParam('subscription_land_usage'))) : [];
            $subscription['project_type_list'] = trim($this->getParam('subscription_project_type')) ? explode(',', trim($this->getParam('subscription_project_type'))) : [];

            $bigDataDeveloperService = new BigDataDeveloperService();
            $rs = $bigDataDeveloperService->getDeveloperScore($organizingCodes, $subscription);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error("actionGetDeveloperScore exception message:" . $ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }

    }
}